package com.unidt.www.framework.common.util.string;

import java.security.SecureRandom;
import java.util.Random;

/**
 *
 * @author sk
 * @date 2018/3/8
 * 随机数字或字符串生成
 */
public class RandomUtils {

    private static final String SYMBOLS_STR = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
    private static final String SYMBOLS = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    private static final String SYMBOLSNUM = "0123456789";
    private static final Random RANDOM = new SecureRandom();

    private   Random strGen = new Random();
    private  Random numGen = new Random();
    private  static  char[] numbersAndLetters = ("0123456789abcdefghijklmnopqrstuvwyzABCDEFGHIJKLMNOPQRSTUVWYZ").toCharArray();
    private  char[] numbers = ("0123456789").toCharArray();
    /** * 产生随机字符串 * */
    public    static String randomStr(int length) {
           Random sstrGen = new Random();


        if (length < 1) {
            return null;
        }
        char[] randBuffer = new char[length];
        for (int i = 0; i < randBuffer.length; i++) {
            randBuffer[i] = numbersAndLetters[sstrGen.nextInt(59)];
        }
        return new String(randBuffer);


    }



    public  static   String randomComonString(int length){

        RandomUtils c = new RandomUtils();

        return  c.randomStr(length);
    }

    public  static   String randomNum(int length){

        RandomUtils c = new RandomUtils();

        return  c.randomNumStr(length);
    }


    /** * 产生随机数值字符串 * */
    public  final String randomNumStr(int length) {
        if (length < 1) {
            return null;
        }
        char[] randBuffer = new char[length];
        for (int i = 0; i < randBuffer.length; i++) {
            randBuffer[i] = numbers[numGen.nextInt(9)];
        }
        return new String(randBuffer);
    }


    /**
     * 随机汉字
     * @return
     */
    public static char getRandomChar() {
        return (char) (0x4e00 + (int) (Math.random() * (0x9fa5 - 0x4e00 + 1)));
    }

    public static String generatesStringNonceStr(Integer length) {
        char[] nonceChars = new char[length];

        for(int index = 0; index < nonceChars.length; ++index) {
            nonceChars[index] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ".charAt(RANDOM.nextInt("ABCDEFGHIJKLMNOPQRSTUVWXYZ".length()));
        }

        return new String(nonceChars);
    }

    public static String generatesNumNonceStr(Integer length) {
        char[] nonceChars = new char[length];

        for(int index = 0; index < nonceChars.length; ++index) {
            nonceChars[index] = "0123456789".charAt(RANDOM.nextInt("0123456789".length()));
        }

        return new String(nonceChars);
    }

    public static String getMixNonceString() {
        StringBuffer buffer = new StringBuffer();
        String str = generatesStringNonceStr(3);
        String num = generatesNumNonceStr(3);
        buffer.append(str);
        buffer.append(num);
        return buffer.toString();
    }

    public static String getMixString(Integer length) {
        char[] nonceChars = new char[length];

        for(int index = 0; index < nonceChars.length; ++index) {
            nonceChars[index] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789".charAt(RANDOM.nextInt("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789".length()));
        }

        return new String(nonceChars);
    }
}
